import { app } from "../../../../../../extensions/app-game/assets/core/app";
import { ecs } from "../../../../../../extensions/app-game/assets/libs/ecs/ECS";
import { GameEvent } from "../../../common/config/GameEvent";
import { GameStorage } from "../../../common/config/GameStorage";
import { Account } from "../Account";
import { AccountModelComp } from "../model/AccountModelComp";

/** 请求玩家游戏数据 */
@ecs.register("AccountNetData")
export class AccountNetDataComp extends ecs.Comp {
  reset() {}
}

/** 请求玩家游戏数据系统 */
@ecs.register("Account")
export class AccountNetDataSystem
  extends ecs.ComblockSystem
  implements ecs.IEntityEnterSystem
{
  filter(): ecs.IMatcher {
    return ecs.allOf(AccountNetDataComp, AccountModelComp);
  }
  entityEnter(e: Account): void {
    let onComplete = {
      target: this,
      callback: (data: any) => {
        // 设置本地存储的用户标识（用于下次登录不输入帐号）
        this.setLocalStorage(data);
        // 玩家登录成功事件
        app.message.dispatchEvent(GameEvent.LoginSuccess);
      },
    };

    // 离线测试代码开始
    var data = {
      id: 2,
    };

    onComplete.callback(data);
    e.remove(AccountNetDataComp);
  }

  /** 设置本地存储的用户标识 */
  private setLocalStorage(data: any) {
    app.storage.setUser(data.id);
    app.storage.set(GameStorage.Account, data);
  }
}
